package com.aat.dxfy.mq.t04;

import java.util.Properties;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;

public class ONSRecvMsg {
	public static void main(String[] args) throws InterruptedException {
		/**
		 * 设置阿里云的AccessKey，用于鉴权
		 */
		final String acessKey = "XXXXXX";
		/**
		 * 设置阿里云的SecretKey，用于鉴权
		 */
		final String secretKey = "XXXXXXX";
		/**
		 * 收消息使用的一级Topic，需要先在MQ控制台里申请
		 */
		final String topic = "MQTTTestTopic";
		/**
		 * ConsumerID ,需要先在MQ控制台里申请
		 */
		final String consumerID = "CID_MQTTTestTopic";
		Properties properties = new Properties();
		properties.put(PropertyKeyConst.ConsumerId, consumerID);
		properties.put(PropertyKeyConst.AccessKey, acessKey);
		properties.put(PropertyKeyConst.SecretKey, secretKey);
		Consumer consumer = ONSFactory.createConsumer(properties);
		/**
		 * 此处MQ客户端只需要订阅MQTT的一级Topic即可
		 */
		consumer.subscribe(topic, "*", new MessageListener() {
			@Override
			public Action consume(Message message, ConsumeContext consumeContext) {
				System.out.println("recv msg:" + message);
				return Action.CommitMessage;
			}
		});
		consumer.start();
		System.out.println("[Case Normal Consumer Init]   Ok");
		Thread.sleep(Integer.MAX_VALUE);
		consumer.shutdown();
		System.exit(0);
	}
}